package org.jf.tool.fx.jftoolfx.main.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import org.jf.tool.fx.jftoolfx.common.BaseController;
import org.jf.tool.fx.jftoolfx.help.controller.AboutController;

public class MainController {

    private final AboutController aboutController = new AboutController();

    @FXML
    private TabPane tabPane;

    @FXML
    private void menuBarCheck(ActionEvent event) {

        MenuItem menuItem = (MenuItem) event.getSource();
        String text = menuItem.getText();
        String id = menuItem.getId();
        // 检查是否已存在同名标签页
        for (Tab tab : tabPane.getTabs()) {
            if (id.equals(tab.getId())) {
                tabPane.getSelectionModel().select(tab);
                return;
            }
        }

        // 创建新标签页
        Tab tab = new Tab(text);
        tab.setId(id);
        tab.setContent(BaseController.getFxml(id));

        // 将新标签页添加到TabPane
        tabPane.getTabs().add(tab);

        // 选中新创建的标签页
        tabPane.getSelectionModel().select(tab);
    }

    @FXML
    private void about() {
        aboutController.show();
    }
}